home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 …ember: Reference Library / Dev.CD Dec 94.toast / What's New? / Sample Code / Snippets Update / Simple Imagecompressor ƒ / OffscreenShell.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-21  |  11.0 KB  |  494 lines  |  [TEXT/KAHL]

  1. // shell for reading pict's into a buffered offscreen window.
  2. // The guts of this is in the window creation function: 
  3. // DoCreateWindow( PicHandle thePicture ), this file also 
  4. // contains a simple main event loop.
  5. //
  6. // Nick Thompson, Summer 1994
  7. //
  8. //    Copyright:    © 1994 by Apple Computer, Inc., all rights reserved.
  9.  
  10.  
  11. #include <menus.h>
  12. #include <PictUtil.h>
  13. #include <QDOffScreen.h>
  14. #include "OffscreenShell.h"
  15. #include "CompressImageTest.h"
  16.  
  17. static     OSType    gCodec = '????';
  18. static     short    gDepth = 0 ;
  19.  
  20.  
  21. main()
  22. {
  23.     InitToolbox() ;
  24.     
  25.     MainEventLoop();
  26. }
  27.  
  28.  
  29.  
  30. void checkForQuickTime()
  31. {
  32.     long    version;
  33.     
  34.     if (Gestalt( gestaltQuickTime, &version ) != noErr)
  35.     {
  36.         ParamText( "\pQuickTime not installed.  Please install, then try again.", "\p", "\p", "\p" );
  37.         Alert( 129, nil );
  38.         ExitToShell();
  39.     }
  40. }
  41.  
  42.  
  43. void InitToolbox(void)
  44. {
  45.     OSErr         retCode;
  46.     long         gestResponse;
  47.     Handle        menuBar = nil;
  48.     EventRecord event;
  49.     short        count;
  50.  
  51.  
  52.     InitGraf((Ptr) &qd.thePort);
  53.     InitFonts();
  54.     InitWindows();
  55.     InitMenus();
  56.     TEInit();
  57.     InitDialogs((long)nil);
  58.     InitCursor();
  59.  
  60.     // initialize application globals
  61.     
  62.     gQuitFlag = false;
  63.     
  64.     
  65.     menuBar = GetNewMBar(128);                // Read menus into menu bar, MBAR res id is 128
  66.     
  67.     if ( menuBar == nil )
  68.          ExitToShell();                        // if we dont have it then quit - your app 
  69.                                              // needs a dialog here
  70.  
  71.     SetMenuBar(menuBar);                    // Install menus
  72.     DisposHandle(menuBar);
  73.     
  74.     AddResMenu(GetMHandle(mApple), 'DRVR');    // Add DA names to Apple menu, ID 128
  75.  
  76.     DrawMenuBar();
  77.     
  78.     checkForQuickTime() ;
  79. }
  80.  
  81.  
  82. void MainEventLoop()
  83. {
  84.     EventRecord     event;
  85.     WindowPtr       window;
  86.     short           thePart;
  87.     Rect            screenRect;
  88.     Point            aPoint = {100, 100};
  89.     GWorldPtr        theNewWorld ;
  90.     PixMapHandle    offPixMap ;
  91.     GrafPtr            oldPort ;
  92.  
  93.     while( !gQuitFlag )
  94.     {
  95.         if (WaitNextEvent( everyEvent, &event, 0, nil ))
  96.         {
  97.             AdjustMenus() ;
  98.  
  99.             switch (event.what) {
  100.                 case mouseDown:
  101.                 
  102.                     thePart = FindWindow( event.where, &window );
  103.                     
  104.                     switch( thePart ) {
  105.                         case inMenuBar: 
  106.                             HandleMenuCommand(MenuSelect(event.where));
  107.                             break;
  108.                         
  109.                         case inDrag:
  110.                     
  111.                             screenRect = (**GetGrayRgn()).rgnBBox;
  112.                             DragWindow( window, event.where, &screenRect );
  113.                             break ;
  114.                     
  115.                         case inContent:
  116.                     
  117.                             if (window != FrontWindow())
  118.                                 SelectWindow( window );
  119.                             break ;
  120.                     
  121.                         case inGoAway:
  122.                             if (TrackGoAway( window, event.where )) {
  123.                                 DisposeWindow ( window );
  124.                             }
  125.                             break ;
  126.                             
  127.                         default:
  128.                             break ;
  129.                     }
  130.                     break ;
  131.                             
  132.                         
  133.                 case updateEvt:
  134.                 
  135.                     window = (WindowPtr)event.message;
  136.                     GetPort(&oldPort ) ;    
  137.                     SetPort( window );
  138.                     
  139.                     BeginUpdate( window );
  140.                     
  141.                     // get the GWorld from the window refcon
  142.                     theNewWorld = (GWorldPtr)GetWRefCon ( window );
  143.                     offPixMap = GetGWorldPixMap( theNewWorld ) ;
  144.                     (void) LockPixels( offPixMap ) ;
  145.                     CopyBits( &((GrafPtr)theNewWorld)->portBits,
  146.                               &window->portBits,
  147.                               &window->portRect,
  148.                               &window->portRect,
  149.                               srcCopy,
  150.                               nil ) ;
  151.                     (void) UnlockPixels( offPixMap ) ;
  152.  
  153.                     EndUpdate( window );
  154.                     SetPort( oldPort ) ;
  155.                     break ;
  156.                     
  157.                 case keyDown:
  158.                 case autoKey:
  159.                     HandleKeyPress(&event);
  160.                     break;
  161.                     
  162.                 case diskEvt:
  163.                     if ( HiWrd(event.message) != noErr ) 
  164.                         (void) DIBadMount(aPoint, event.message);
  165.                     break;
  166.                     
  167.                 case osEvt:
  168.                 case activateEvt:
  169.                     break;
  170.  
  171.  
  172.             }
  173.         }
  174.     }
  175. }
  176.  
  177.  
  178. void HandleKeyPress(EventRecord *event)
  179. {
  180.     char    key;
  181.  
  182.     key = event->message & charCodeMask;
  183.     
  184.     // just check to see if we want to quit...
  185.     
  186.     if ( event->modifiers & cmdKey ) {        /* Command key down? */
  187.         HandleMenuCommand(MenuKey(key));
  188.     } 
  189. }
  190.  
  191.  
  192. void HandleMenuCommand(long menuResult)
  193. {
  194.     short        menuID;
  195.     short        menuItem;
  196.     Str255        daName;
  197.     DialogPtr    theDialog ; 
  198.     short        itemHit ;
  199.     SFTypeList    myTypes = { 'PICT' } ;
  200.     FSSpec        theFSSpec ;
  201.     PicHandle    thePicture ;
  202.     OSErr        err ;
  203.     short        theRef ;
  204.     
  205.     StandardFileReply    theSFReply ;
  206.  
  207.     menuID = HiWrd(menuResult);
  208.     menuItem = LoWrd(menuResult);
  209.     switch ( menuID ) {
  210.         case mApple:
  211.             switch ( menuItem ) {
  212.                 case iAbout:
  213.                     theDialog = GetNewDialog ( 128, nil, (WindowPtr)-1 );
  214.                     do {
  215.                         ModalDialog ( nil, &itemHit );
  216.                     } while( itemHit != ok ) ;
  217.                     DisposDialog ( theDialog );
  218.                     break;
  219.                     
  220.                 default:
  221.                     GetItem(GetMHandle(mApple), menuItem, daName);
  222.                     (void) OpenDeskAcc(daName);
  223.                     break;
  224.             }
  225.             break;
  226.         case mFile:
  227.             switch ( menuItem ) {
  228.                 case iOpen:
  229.                     // Get the file name to open
  230.                     StandardGetFilePreview( nil, 2, myTypes, &theSFReply ) ;
  231.                     
  232.                     // did the user cancel?
  233.                     if(!theSFReply.sfGood)
  234.                         break ;
  235.                     
  236.                     // open the file
  237.                     err = FSpOpenDF( &theSFReply.sfFile, fsRdPerm, &theRef ) ;
  238.                     
  239.                     if( err != noErr )
  240.                         break ;     // should handle this properly
  241.                         
  242.                     thePicture = DoReadPICT( theRef, &err ) ;
  243.                     
  244.                     if( err != noErr )
  245.                         break ;     // should handle this properly
  246.                 
  247.                     // display the contents
  248.                     err = DoCreateWindow( thePicture ) ;
  249.  
  250.                     break ;
  251.                     
  252.                 case iClose:
  253.                     DisposeWindow ( FrontWindow() );
  254.                     break ;
  255.                     
  256.                 case iSave:
  257.                     DoSaveAs( FrontWindow() ) ;
  258.                     break ;
  259.                     
  260.                 case iQuit:
  261.                     gQuitFlag = true;
  262.                     
  263.                     while( FrontWindow())
  264.                         DisposeWindow ( FrontWindow() );
  265.  
  266.                     break;
  267.             }
  268.             break;
  269.             
  270.         case mPalette:
  271.             switch ( menuItem ) {
  272.                 case iUsePictPalette :
  273.                     // toggle the check mark and the global boolean
  274.                     gUsePictPalette = !gUsePictPalette ;
  275.                     CheckItem ( GetMHandle ( mPalette ), iUsePictPalette, gUsePictPalette );
  276.                     break ;
  277.             }
  278.             break ;
  279.     }
  280.     HiliteMenu(0);        // Unhighlight whatever MenuSelect or MenuKey hilited
  281. }
  282.  
  283. void AdjustMenus( void ) 
  284. {
  285.     WindowPtr    theWindow ;
  286.     theWindow = FrontWindow() ;
  287.     if( theWindow != nil ) {
  288.         EnableItem ( GetMHandle ( mFile ), iClose );
  289.     }
  290.     else {
  291.         DisableItem ( GetMHandle ( mFile ), iClose );
  292.     }
  293.     // make sure the check marks are correct
  294.     CheckItem ( GetMHandle ( mPalette ), iUsePictPalette, gUsePictPalette );
  295. }
  296.  
  297. PicHandle DoReadPICT( short theRef, OSErr *theErr ) 
  298. {
  299.     long        theFileSize ;
  300.     PicHandle    thePicture ;
  301.     
  302.     // pict files have a 512 byte header at the front - we dont care about this
  303.     // we can find the size of the pict by subtracting 512 bytes from the length
  304.     // of the file.  We then want to resize the handle to that and read the data
  305.     // into the resized handle.
  306.     
  307.     if(( *theErr = GetEOF( theRef, &theFileSize )) != noErr ) {
  308.         FSClose( theRef ) ;
  309.         return nil ; 
  310.     }
  311.     
  312.     if(( *theErr = SetFPos( theRef, fsFromStart, 512)) != noErr ) {
  313.         FSClose( theRef ) ;
  314.         return nil ; 
  315.     }
  316.  
  317.     theFileSize -= 512 ;
  318.     
  319.     thePicture = (PicHandle)NewHandle( theFileSize ) ;
  320.     if( thePicture == nil ) {
  321.         FSClose( theRef ) ;
  322.         *theErr = MemError() ;
  323.         return nil ;         // what ever the mem manager error was
  324.     }
  325.     
  326.     HLock( (Handle)thePicture ) ;
  327.     *theErr = FSRead( theRef, &theFileSize, (Ptr)*thePicture ) ;
  328.     HUnlock(  (Handle)thePicture ) ;
  329.     
  330.     if( *theErr != noErr ) {
  331.         FSClose( theRef ) ;
  332.         return nil ; 
  333.     }
  334.  
  335.     return thePicture ;    
  336. }
  337.  
  338.  
  339. pascal void myStdPix( PixMapPtr src, Rect *srcRect, MatrixRecordPtr matrix,
  340.                         short mode, RgnHandle mask, PixMapPtr matte,
  341.                         Rect *matteRect, short flags )
  342. {
  343.     ImageDescriptionHandle        desc;
  344.     Ptr                            data;
  345.     long                        bufferSize;
  346.     
  347.     GetCompressedPixMapInfo( src, &desc, &data, &bufferSize, nil, nil );
  348.     gCodec = (**desc).cType;
  349.     gDepth = (**desc).depth;
  350. }
  351.  
  352. pascal void myTextProc( short byteCount, Ptr textBuf, Point numer, Point denom )
  353. {
  354. }
  355.  
  356. pascal void myLineProc( Point newPt )
  357. {
  358. }
  359.  
  360. pascal void myRectProc( GrafVerb verb, Rect *r )
  361. {
  362. }
  363.  
  364. pascal void myRRectProc( GrafVerb verb, Rect *r, short ovalWidth, short ovalHeight )
  365. {
  366. }
  367.  
  368. pascal void myOvalProc( GrafVerb verb, Rect *r )
  369. {
  370. }
  371.  
  372. pascal void myArcProc( GrafVerb verb, Rect *r, short startAngle, short arcAngle )
  373. {
  374. }
  375.  
  376. pascal void myPolyProc( GrafVerb verb, PolyHandle poly )
  377. {
  378. }
  379.  
  380. pascal void myRgnProc( GrafVerb verb, RgnHandle rgn )
  381. {
  382. }
  383.  
  384. pascal void myBitsProc( BitMap *bitPtr, Rect *srcRect, Rect *dstRect,
  385.                         short mode, RgnHandle maskRgn )
  386. {
  387. }
  388.  
  389.  
  390. void doBottleneckTest(    PicHandle    picture )
  391. {
  392.     int            i;
  393.     CQDProcs    bottlenecks;
  394.  
  395.     /* Define our own bottlenecks to do nothing. */
  396.     SetStdCProcs( &bottlenecks );
  397.  
  398.     bottlenecks.textProc    = (QDTextUPP)myTextProc;
  399.     bottlenecks.lineProc    = (QDLineUPP)myLineProc;
  400.     bottlenecks.rectProc    = (QDRectUPP)myRectProc;
  401.     bottlenecks.rRectProc    = (QDRRectUPP)myRRectProc;
  402.     bottlenecks.ovalProc    = (QDOvalUPP)myOvalProc;
  403.     bottlenecks.arcProc        = (QDArcUPP)myArcProc;
  404.     bottlenecks.polyProc    = (QDPolyUPP)myPolyProc;
  405.     bottlenecks.rgnProc        = (QDRgnUPP)myRgnProc;
  406.     bottlenecks.bitsProc    = (QDBitsUPP)myBitsProc;
  407.     bottlenecks.newProc1    = (UniversalProcPtr)myStdPix;        /* pixProc */
  408.     
  409.     /* Load & draw pictures from resource. */
  410.     picture = (PicHandle)Get1IndResource( 'PICT', i + 1 );
  411.     
  412.     /* Install our custom bottlenecks to intercept any compressed images. */
  413.     (*(qd.thePort)).grafProcs = (QDProcs *)&bottlenecks;
  414.     DrawPicture( picture, &((**picture).picFrame) );
  415.              
  416.     /* Switch back to the default procs. */
  417.     (*(qd.thePort)).grafProcs = 0L;
  418. }
  419.  
  420.  
  421. OSErr DoCreateWindow( PicHandle thePicture )
  422. {
  423.  
  424.     Rect        theRect ;
  425.     OSErr        theErr ;
  426.     GWorldPtr    theNewWorld ;
  427.     CGrafPtr    savedPort ;
  428.     GWorldPtr    savedGWorld ;
  429.     WindowPtr    theWindow ;
  430.     GDHandle    oldDevice ;
  431.     
  432.     PictInfo        thePictInfo ;
  433.     PaletteHandle    thePictPalette = nil ;
  434.     CTabHandle        thePictCTab = nil ;
  435.     
  436.     // make an offscreen environment and image the pict into this
  437.     // Make a window the size of the pict
  438.     // store a reference to the GWorld in the Refcon of the window
  439.     // invalidate the window content area.
  440.     
  441.     theRect.top = (**thePicture).picFrame.top ;
  442.     theRect.left = (**thePicture).picFrame.left ;
  443.     theRect.bottom = (**thePicture).picFrame.bottom ;
  444.     theRect.right = (**thePicture).picFrame.right ;
  445.     
  446.     doBottleneckTest( thePicture ) ;
  447.     thePictInfo.depth = gDepth ;
  448.     
  449.     theErr = NewGWorld( &theNewWorld, thePictInfo.depth, &theRect, thePictCTab, nil, 0L ) ;
  450.     
  451.     if( theErr != noErr ) 
  452.         return theErr ;
  453.     
  454.     // save the world
  455.     GetGWorld( &savedPort, &oldDevice ) ;
  456.     SetGWorld( theNewWorld, nil ) ;
  457.     
  458.     
  459.     RGBForeColor( &kRGBBlack ) ;        // ensure the fg and bg colors are 
  460.     RGBBackColor( &kRGBWhite ) ;        // as anticipated
  461.     EraseRect( &theRect ) ;                // clear the area for the pict
  462.     PenMode( srcCopy ) ;                // ensure the t/f mode is as expected
  463.  
  464.     // render the image into the offscreen buffer
  465.     DrawPicture( thePicture, &theRect ) ;
  466.     
  467.     SetGWorld( savedPort, oldDevice ) ;
  468.     
  469.     // create the window
  470.     OffsetRect( &theRect, gStaggerPos.h, gStaggerPos.v) ;
  471.     gStaggerPos.h += 16 ;
  472.     gStaggerPos.v += 16 ;        // heh - should roll these around, but you wont 
  473.                                 // create more than a couple of windows, will you  :-)
  474.                                  
  475.     theWindow  = NewCWindow( nil, &theRect, "\pplayTime", true, 
  476.                                 documentProc, (WindowPtr)-1, true, (long)theNewWorld );    
  477.                 
  478.     // and if we set up the palette earlier assign it to the window                
  479.     if( thePictPalette != nil ) {
  480.         SetPalette ( theWindow, thePictPalette, true );
  481.     }
  482.     
  483.     ActivatePalette ( theWindow );    
  484.     
  485.     // make sure it is visible
  486.     ShowWindow( theWindow ) ;
  487.     
  488.     SetGWorld( (CGrafPtr)theWindow, nil ) ;
  489.     
  490.     // invalidate the content region of the window - we don't do any drawing to it here.
  491.     InvalRect ( &theRect );
  492.     
  493.     SetGWorld( savedPort, oldDevice ) ;
  494. }